﻿

namespace Mulala.Cavan.Presentation.Mvc.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Domain.MainBoundedContext.InformationModule;
    using Spring.Context;
    using Spring.Context.Support;
    using Mulala.Cavan.Application.MainBoundedContext.AttachmentModule;
    using Mulala.Cavan.Application.MainBoundedContext.MulalaModule;
    using Mulala.Cavan.Presentation.Seedwork;
    using Mulala.Cavan.Domain.MainBoundedContext.SecurityModule;
    using Mulala.Cavan.Domain.MainBoundedContext.ProprietorModule;
    using Mulala.Cavan.Domain.MainBoundedContext.NannyModule;
    using Mulala.Cavan.Domain.MainBoundedContext.CorporationModule;

    public class UserMessageViewModelConverter
    {
        public static IList<IUserMessageViewModel> Convert(IList<UserMessage> userMessages)
        {
            IApplicationContext springContext = ContextRegistry.GetContext();
            IAttachmentAppService attachmentAppService = springContext.GetObject(MulalaAppServiceConst.ATTACHMENTAPPSERVICE) as IAttachmentAppService;  
      
            IList<IUserMessageViewModel> userMessageViewModels = new List<IUserMessageViewModel>();
            foreach (UserMessage um in userMessages)
            {
                IUserMessageViewModel userMessageViewModel = ModelFactory.CreateModel<IUserMessageViewModel>();
                userMessageViewModel.Item = um;
                if ((SecurityIdentifier.Role)um.MessagerUser.Roles[0].ID == SecurityIdentifier.Role.Proprietor)
                {
                    userMessageViewModel.Avatar = attachmentAppService.GetProprietorAvatar(ProprietorFactory.CreateProprietor(um.MessagerUser.ID));
                }
                else if ((SecurityIdentifier.Role)um.MessagerUser.Roles[0].ID == SecurityIdentifier.Role.Nanny)
                {
                    userMessageViewModel.Avatar = attachmentAppService.GetNannyAvatar(NannyFactory.CreateNanny(um.MessagerUser.ID));
                }
                else if ((SecurityIdentifier.Role)um.MessagerUser.Roles[0].ID == SecurityIdentifier.Role.Corporation)
                {
                    userMessageViewModel.Avatar = attachmentAppService.GetCorporationLogo(CorporationFactory.CreateCorporation(um.MessagerUser.ID));
                }
                userMessageViewModels.Add(userMessageViewModel);
            }

            return userMessageViewModels;
        }
    }
}
